package gui;

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Dimension;

import javax.swing.JScrollPane;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;

public abstract class MarcoAbstracto extends JFrame {

	private JPanel contentPane;
	private JPanel header;
	private JScrollPane content;
	private JPanel footer;
	private JLabel lblCopyrightSummertime;

	/**
	 * Create the frame.
	 */
	public MarcoAbstracto() {
		setPreferredSize(new Dimension(1024, 768));
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1000, 606);
		contentPane = new JPanel();
		contentPane.setPreferredSize(new Dimension(1024, 768));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		header = new JPanel();
		contentPane.add(header, BorderLayout.NORTH);
		header.setLayout(new BorderLayout(0, 0));
		
		header.add(new VPublicidad());
		
		content = new JScrollPane();
		content.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
		content.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener(){
			@Override
			public void adjustmentValueChanged(AdjustmentEvent e) {
				SwingUtilities.invokeLater(new Runnable(){
					@Override
					public void run() {
						content.updateUI();
					}
				});
			}
		});
		contentPane.add(content, BorderLayout.CENTER);
		
		//content.setViewportView(new VPaginaPrin());
		//content.setViewportView(new VReproductor());
		//content.setViewportView(new VComentarios());
		//content.setViewportView(new VSelecTema());
		VnVideo vnVideo = new VnVideoPropio();
		content.setViewportView(vnVideo);
		footer = new JPanel();
		contentPane.add(footer, BorderLayout.SOUTH);
		
		lblCopyrightSummertime = new JLabel("\u00A92012 SUMMERTIME");
		footer.add(lblCopyrightSummertime);
	}

	public JPanel getPanel() {
		return header;
	}
	
	protected void removeCenterComponent(){
		BorderLayout layout = (BorderLayout) contentPane.getLayout();
		Component c = layout.getLayoutComponent(BorderLayout.CENTER);
		contentPane.remove(c);	
		content = new JScrollPane();
		content.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
		content.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener(){
			@Override
			public void adjustmentValueChanged(AdjustmentEvent e) {
				SwingUtilities.invokeLater(new Runnable(){
					@Override
					public void run() {
						content.updateUI();
					}
				});
			}
		});
		contentPane.add(content, BorderLayout.CENTER);
	}
	
	protected void mostrarPaginaPrincipal(){
		removeCenterComponent();
		content.setViewportView(new VPaginaPrin());
		refrescar();
	}
	
	protected JScrollPane getContent(){
		return content;
	}
	
	public void refrescar(){
		validate();
		repaint();
	}

	protected void buscar(String text, boolean selected) {
		removeCenterComponent();
		content.setViewportView(new VResultadoBusqueda(text,selected));
		refrescar();
		
	}
}
